#include "widget.h"
#include "ui_widget.h"

#include <QRandomGenerator>
#include <QTimer>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //设置没边框
    this->setWindowFlag(Qt::FramelessWindowHint);
    //设置窗口透明
    this->setAttribute(Qt::WA_TranslucentBackground);

    //获取随机数
    // int rand=QRandomGenerator::global()->bounded(0,100);
    // qDebug()<<rand;
    //设置窗口最大化
    this->showMaximized();
    QTimer* qtm=new QTimer(this);
    connect(qtm,&QTimer::timeout,this,[=](){
        flag=!flag;
        int rand=QRandomGenerator::global()->bounded(0,100);
        x+=rand%20;
        y+=rand%20;
        if(x>this->width())
        {
            x=0;
        }
        if(y>this->height())
        {
            y=0;
        }
        update();
    });

    up.load(":/Image1/up.png");
    down.load(":/Image1/down.png");
    flag=false;
    int rand=QRandomGenerator::global()->bounded(0,100);
    x=rand%(this->width());
    y=rand%(this->height());
    qtm->start(100);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    // p.drawPixmap(0,0,QPixmap(":/Image1/bk.png"));
    if(flag)
    {
        p.drawPixmap(x,y,up);
    }
    else
    {
        p.drawPixmap(x,y,down);
    }

}

void Widget::mousePressEvent(QMouseEvent *ev)
{
    //
    if(ev->button()==Qt::LeftButton)
    {
        //求差值   左键按下的点-窗口左上角点的坐标
        pt=ev->globalPos()-this->frameGeometry().topLeft();
    }
    else if(ev->button()==Qt::RightButton)
    {
        //关闭窗口
        this->close();
    }
}

void Widget::mouseMoveEvent(QMouseEvent *ev)
{
    this->move(ev->globalPos()-pt);
}












